package com.tiyende.credit.loop.sdk.dec

import com.google.gson.Gson
import com.tiyende.credit.loop.TiyendeApp
import com.tiyende.credit.loop.configs.TiyendeConfigs
import com.tiyende.credit.loop.sdk.dec.userdec.TiyendeUserDecType1
import com.tiyende.credit.loop.sdk.dec.userdec.TiyendeUserDecType2
import com.tiyende.credit.loop.sdk.dec.userdec.TiyendeUserDecType3
import com.tiyende.credit.loop.sdk.dec.userdec.TiyendeUserDecType4
import com.tiyende.credit.loop.sdk.dec.userdec.TiyendeUserDecType5
import com.tiyende.credit.loop.sdk.dec.userdec.TiyendeUserDecType6
import com.tiyende.credit.loop.sdk.dec.userdec.TiyendeUserDecType7
import com.tiyende.credit.loop.utils.shared.TiyendeSharedHelper
import com.tiyende.credit.loop.utils.shared.TiyendeSharedKey

class TiyendeUserDecManager() {
    val context = TiyendeApp.app
    val product = TiyendeConfigs.TIYENDE_APPNAME
    val token = TiyendeSharedHelper.queryTiyendeSharedString(TiyendeSharedKey.TIYENDE_TOKEN)

    private fun parameter1(path: String, fetchData: () -> String?): String? {
        if (!formatJson(path)) return null
        val content = fetchData()?:""

        // TODO: 加密操作
//        val encrypted: String = EKHAesTool.encryptDefault(content)
//        return encrypted
        return content
    }

    private fun parameter2(path: String, key: String, fetchData: () -> String?): String? {
        if (!formatJson(path)) return null
        val data = fetchData() ?: return null
        val params = commonParameter(path).apply { put(key, data) }
        val content = Gson().toJson(params).trim()

        // TODO: 加密操作
//        val encrypted: String = EKHAesTool.encryptDefault(content)
//        return encrypted
        return content
    }

    private fun formatJson(path: String) = listOf(product, token, path).all { it.isNotBlank() }

    private fun commonParameter(path: String) = hashMapOf<String, Any>(
        "path" to path.replace("/", ""),
        "token" to token,
        "product" to product
    )

    fun getSubmitDec1(path: String) = parameter1(path) {
        TiyendeUserDecType1(context, product, token, path).getUserDecType1Data()
    }

    fun getSubmitDec7(path: String) = parameter1(path) {
        TiyendeUserDecType7(context, product, token, path).getUserDecType7Data()
    }

    fun getSubmitDec3(path: String) = parameter2(path, "deviceHardwareGzip") {
        TiyendeUserDecType3(context).getUserDecType3Data()
    }

    fun getSubmitDec2(path: String, onResult: (String?) -> Unit) {
        TiyendeUserDecType2(context = context, onResult = { bd ->
            val result = parameter2(path, "deviceBatteryGzip") { bd }
            onResult.invoke(result)
        }).start()
    }

    fun getSubmitDec5(path: String) = parameter2(path, "deviceWifiGzip") {
        TiyendeUserDecType5(context).getUserDecType5Data()
    }

    fun getSubmitDec4(path: String) = parameter2(path, "deviceStorageGzip") {
        TiyendeUserDecType4(context).getUserDecType4Data()
    }

    fun getSubmitDec6(path: String) = parameter2(path, "appListGzip") {
        TiyendeUserDecType6(context).getUserDecType6Data()
    }
}